import { requestClient } from '#/api/request'
import { LocaleDictionaryPrefix } from '#/config'
import { treeToList } from '#/helper/treeHelper'
import { localesMap } from '#/locales'
import { localesTreeOptions, toLocalesTreeOptions } from '#/utils/localesConfig'
import type { ApiPaginationParams } from '#types/utils'

/**
 * 字典
 */
export default class DictionaryApi {
    static get inst() {
        return DictionaryApi._inst || (DictionaryApi._inst = new DictionaryApi())
    }
    private static _inst: DictionaryApi

    /**
     * 新增
     * @param params
     */
    add(params: Partial<any>) {
        return requestClient.post<null>('/dict/add', params)
    }

    /**
     * 批量删除
     * @param ids
     */
    delete(ids: Array<number | string>) {
        return requestClient.delete<null>(`/dict/delete/${ids}`)
    }
    /**
     * 编辑
     * @param id
     * @param params
     */
    edit(id: number | string, params: Partial<any>) {
        return requestClient.put<null>(`/dict/edit`, { id, ...params })
    }

    /**
     * 获取所有国际语言
     */
    async getI18nAll() {
        await new Promise((r) => setTimeout(r, 1000))
        const arr: any[] = []
        localesTreeOptions.value.forEach((v) => {
            arr.push(...treeToList(v.children))
        })

        // eslint-disable-next-line unicorn/no-await-expression-member
        const zh = treeToList(toLocalesTreeOptions((await localesMap['zh-CN']?.())?.default || []))
        // eslint-disable-next-line unicorn/no-await-expression-member
        const en = treeToList(toLocalesTreeOptions((await localesMap['en-US']?.())?.default || []))
        const resArr = arr.map((v: any) => {
            return {
                id: v.value,
                // 添加前缀 避免其他字典用不了
                code: `${LocaleDictionaryPrefix}${v.value}`,
                name: v.label,
                itemList: [
                    {
                        name: zh.find((v2: any) => v2.value === v.value)?.label || '',
                        id: 11,
                        createUser: 1,
                        createTime: '2025-03-16 08:10:28',
                        updateUser: 1,
                        updateTime: '2025-03-17 09:57:38',
                        mark: 1,

                        code: 'CHN',
                        dictId: 4,
                        status: 1,
                        note: '',
                        sort: 1
                    },
                    {
                        name: en.find((v2: any) => v2.value === v.value)?.label || '',
                        id: 12,
                        createUser: 1,
                        createTime: '2025-03-16 08:11:21',
                        updateUser: 1,
                        updateTime: '2025-03-17 09:57:47',
                        mark: 1,

                        code: 'USA',
                        dictId: 4,
                        status: 1,
                        note: '',
                        sort: 2
                    }
                ]
            }
        })

        // const resArr = await  requestClient.get<any>('/dict/all')
        // 添加前缀 避免其他字典用不了
        return resArr.filter((v: any) => v.code.startsWith(LocaleDictionaryPrefix))
    }

    /**
     * 获取字典列表
     */
    getPageList(params: ApiPaginationParams<undefined>) {
        return requestClient.get<any>('/dict/index', {
            params
        })
    }
}
